/*** konami: Portable Konami cpu emulator ******************************************

    Copyright Nicola Salmoria and the MAME Team

    Based on M6809 cpu core copyright John Butler

	Port to SMSE2K by unknownfile

    References:

        6809 Simulator V09, By L.C. Benschop, Eidnhoven The Netherlands.

        m6809: Portable 6809 emulator, DS (6809 code in MAME, derived from
            the 6809 Simulator V09)

        6809 Microcomputer Programming & Interfacing with Experiments"
            by Andrew C. Staugaard, Jr.; Howard W. Sams & Co., Inc.

    System dependencies:    UINT16 must be 16 bit unsigned int
                            UINT8 must be 8 bit unsigned int
                            UINT32 must be more than 16 bits
                            arrays up to 65536 bytes must be supported
                            machine must be twos complement

    History:
991022 HJB:
    Tried to improve speed: Using bit7 of cycles1 as flag for multi
    byte opcodes is gone, those opcodes now instead go through opcode2().
    Inlined fetch_effective_address() into that function as well.
    Got rid of the slow/fast flags for stack (S and U) memory accesses.
    Minor changes to use 32 bit values as arguments to memory functions
    and added defines for that purpose (e.g. X = 16bit XD = 32bit).

990720 EHC:
    Created this file

*****************************************************************************/

#include "smse2kincludes.h"
#include "Konami.h"
#include "cpufactory.h"


/* flag bits in the cc register */
#define CC_C    0x01        /* Carry */
#define CC_V    0x02        /* Overflow */
#define CC_Z    0x04        /* Zero */
#define CC_N    0x08        /* Negative */
#define CC_II   0x10        /* Inhibit IRQ */
#define CC_H    0x20        /* Half (auxiliary) carry */
#define CC_IF   0x40        /* Inhibit FIRQ */
#define CC_E    0x80        /* entire state pushed */


/* Konami registers */
#define	pPPC    this->ppc
#define pPC 	this->pc
#define pU		this->u
#define pS		this->s
#define pX		this->x
#define pY		this->y
#define pD		this->d

#define	PPC		this->ppc.w.l
#define PC  	this->pc.w.l
#define PCD 	this->pc.d
#define U		this->u.w.l
#define UD		this->u.d
#define S		this->s.w.l
#define SD		this->s.d
#define X		this->x.w.l
#define XD		this->x.d
#define Y		this->y.w.l
#define YD		this->y.d
#define D   	this->d.w.l
#define A   	this->d.b.h
#define B		this->d.b.l
#define DP		this->dp.b.h
#define DPD 	this->dp.d
#define CC  	this->cc

#define EAB		this->ea.b.l
#define EA		this->ea.w.l
#define EAD 	this->ea.d
#define KONAMI_CWAI		8	/* set when CWAI is waiting for an interrupt */
#define KONAMI_SYNC		16	/* set when SYNC is waiting for an interrupt */
#define KONAMI_LDS		32	/* set when LDS occured at least once */

#define SIGNED(a)	(UINT16)(INT16)(INT8)(a)

/* macros to access memory */
#define IMMBYTE(cs,b)	{ b = ROP_ARG(cs,PCD); PC++; }
#define IMMWORD(cs,w)	{ w.d = (ROP_ARG(cs,PCD)<<8) | ROP_ARG(cs,PCD+1); PC += 2; }

#define PUSHBYTE(cs,b) --S; WM(cs,SD,b)
#define PUSHWORD(cs,w) --S; WM(cs,SD,w.b.l); --S; WM(cs,SD,w.b.h)
#define PULLBYTE(cs,b) b=RM(cs,SD); S++
#define PULLWORD(cs,w) w=RM(cs,SD)<<8; S++; w|=RM(cs,SD); S++

#define PSHUBYTE(cs,b) --U; WM(cs,UD,b);
#define PSHUWORD(cs,w) --U; WM(cs,UD,w.b.l); --U; WM(cs,UD,w.b.h)
#define PULUBYTE(cs,b) b=RM(cs,UD); U++
#define PULUWORD(cs,w) w=RM(cs,UD)<<8; U++; w|=RM(cs,UD); U++

#define CLR_HNZVC	CC&=~(CC_H|CC_N|CC_Z|CC_V|CC_C)
#define CLR_NZV 	CC&=~(CC_N|CC_Z|CC_V)
#define CLR_NZ		CC&=~(CC_N|CC_Z)
#define CLR_HNZC	CC&=~(CC_H|CC_N|CC_Z|CC_C)
#define CLR_NZVC	CC&=~(CC_N|CC_Z|CC_V|CC_C)
#define CLR_Z		CC&=~(CC_Z)
#define CLR_NZC 	CC&=~(CC_N|CC_Z|CC_C)
#define CLR_ZC		CC&=~(CC_Z|CC_C)

/* macros for CC -- CC bits affected should be reset before calling */
#define SET_Z(a)		if(!a)SEZ
#define SET_Z8(a)		SET_Z((UINT8)a)
#define SET_Z16(a)		SET_Z((UINT16)a)
#define SET_N8(a)		CC|=((a&0x80)>>4)
#define SET_N16(a)		CC|=((a&0x8000)>>12)
#define SET_H(a,b,r)	CC|=(((a^b^r)&0x10)<<1)
#define SET_C8(a)		CC|=((a&0x100)>>8)
#define SET_C16(a)		CC|=((a&0x10000)>>16)
#define SET_V8(a,b,r)	CC|=(((a^b^r^(r>>1))&0x80)>>6)
#define SET_V16(a,b,r)	CC|=(((a^b^r^(r>>1))&0x8000)>>14)

static const UINT8 flags8i[256]=	 /* increment */
{
CC_Z,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
CC_N|CC_V,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,
CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,
CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,
CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,
CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,
CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,
CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,
CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N
};
static const UINT8 flags8d[256]= /* decrement */
{
CC_Z,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,CC_V,
CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,
CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,
CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,
CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,
CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,
CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,
CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,
CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N,CC_N
};
#define SET_FLAGS8I(a)		{CC|=flags8i[(a)&0xff];}
#define SET_FLAGS8D(a)		{CC|=flags8d[(a)&0xff];}

/* combos */
#define SET_NZ8(a)			{SET_N8(a);SET_Z(a);}
#define SET_NZ16(a)			{SET_N16(a);SET_Z(a);}
#define SET_FLAGS8(a,b,r)	{SET_N8(r);SET_Z8(r);SET_V8(a,b,r);SET_C8(r);}
#define SET_FLAGS16(a,b,r)	{SET_N16(r);SET_Z16(r);SET_V16(a,b,r);SET_C16(r);}

/* macros for addressing modes (postbytes have their own code) */
#define DIRECT(cs)	EAD = DPD; IMMBYTE(cs,EAB)
#define IMM8(cs)	EAD = PCD; PC++
#define IMM16(cs)	EAD = PCD; PC+=2
#define EXTENDED(cs) IMMWORD(cs,(cs)->ea)

/* macros to set status flags */
#if defined(SEC)
#undef SEC
#endif
#define SEC CC|=CC_C
#define CLC CC&=~CC_C
#define SEZ CC|=CC_Z
#define CLZ CC&=~CC_Z
#define SEN CC|=CC_N
#define CLN CC&=~CC_N
#define SEV CC|=CC_V
#define CLV CC&=~CC_V
#define SEH CC|=CC_H
#define CLH CC&=~CC_H

/* macros for convenience */
#define DIRBYTE(cs,b) DIRECT(cs); b=RM(cs,EAD)
#define DIRWORD(cs,w) DIRECT(cs); w.d=RM16(EAD)
#define EXTBYTE(cs,b) EXTENDED(cs); b=RM(cs,EAD)
#define EXTWORD(cs,w) EXTENDED(cs); w.d=RM16(EAD)

/* macros for branch instructions */
#define BRANCH(cs,f) { 					\
	UINT8 t;							\
	IMMBYTE(cs,t); 						\
	if( f ) 							\
	{									\
		PC += SIGNED(t);				\
	}									\
}

#define LBRANCH(cs,f) {                 \
	PAIR t; 							\
	IMMWORD(cs,t); 						\
	if( f ) 							\
	{									\
		this->icount -= 1;			\
		PC += t.w.l;					\
	}									\
}

#define NXORV  ((CC&CC_N)^((CC&CC_V)<<2))

/* macros for setting/getting registers in TFR/EXG instructions */
#define GETREG(val,reg) 				\
	switch(reg) {						\
	case 0: val = A;	break;			\
	case 1: val = B; 	break; 			\
	case 2: val = X; 	break;			\
	case 3: val = Y;	break; 			\
	case 4: val = S; 	break; /* ? */	\
	case 5: val = U;	break;			\
	default: val = 0xff; logerror("Unknown TFR/EXG idx at PC:%04x\n", PC ); break; \
}

#define SETREG(val,reg) 				\
	switch(reg) {						\
	case 0: A = val;	break;			\
	case 1: B = val;	break;			\
	case 2: X = val; 	break;			\
	case 3: Y = val;	break;			\
	case 4: S = val;	break; /* ? */	\
	case 5: U = val; 	break;			\
	default: logerror("Unknown TFR/EXG idx at PC:%04x\n", PC ); break; \
}

/* opcode timings */
static const UINT8 cycles1[] =
{
	/*   0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F */
  /*0*/  1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 5, 5, 5, 5,
  /*1*/  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  /*2*/  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  /*3*/  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 7, 6,
  /*4*/  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 3, 3, 4, 4,
  /*5*/  4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 1, 1, 1,
  /*6*/  3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5,
  /*7*/  3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5,
  /*8*/  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5,
  /*9*/  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6,
  /*A*/  2, 2, 2, 4, 4, 4, 4, 4, 2, 2, 2, 2, 3, 3, 2, 1,
  /*B*/  3, 2, 2,11,22,11, 2, 4, 3, 3, 3, 3, 3, 3, 3, 3,
  /*C*/  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 2,
  /*D*/  2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  /*E*/  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  /*F*/  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};

#include "Konami_Ops.h"

// Build Op Tables
int CKonamiCPU::Init(int clock) {
	
	void (CKonamiCPU::*konami_main[0x100])() = {
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* 00 */
		&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,&CKonamiCPU::pshs   ,&CKonamiCPU::pshu   ,&CKonamiCPU::puls   ,&CKonamiCPU::pulu   ,
		&CKonamiCPU::lda_im ,&CKonamiCPU::ldb_im ,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,&CKonamiCPU::adda_im,&CKonamiCPU::addb_im,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,	/* 10 */
		&CKonamiCPU::adca_im,&CKonamiCPU::adcb_im,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,&CKonamiCPU::suba_im,&CKonamiCPU::subb_im,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,
		&CKonamiCPU::sbca_im,&CKonamiCPU::sbcb_im,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,&CKonamiCPU::anda_im,&CKonamiCPU::andb_im,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,	/* 20 */
		&CKonamiCPU::bita_im,&CKonamiCPU::bitb_im,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,&CKonamiCPU::eora_im,&CKonamiCPU::eorb_im,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,
		&CKonamiCPU::ora_im ,&CKonamiCPU::orb_im ,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,&CKonamiCPU::cmpa_im,&CKonamiCPU::cmpb_im,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,	/* 30 */
		&CKonamiCPU::setline_im,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,&CKonamiCPU::andcc,&CKonamiCPU::orcc  ,&CKonamiCPU::exg    ,&CKonamiCPU::tfr    ,
		&CKonamiCPU::ldd_im ,&CKonamiCPU::opcode2,&CKonamiCPU::ldx_im ,&CKonamiCPU::opcode2,&CKonamiCPU::ldy_im ,&CKonamiCPU::opcode2,&CKonamiCPU::ldu_im ,&CKonamiCPU::opcode2,	/* 40 */
		&CKonamiCPU::lds_im ,&CKonamiCPU::opcode2,&CKonamiCPU::cmpd_im,&CKonamiCPU::opcode2,&CKonamiCPU::cmpx_im,&CKonamiCPU::opcode2,&CKonamiCPU::cmpy_im,&CKonamiCPU::opcode2,
		&CKonamiCPU::cmpu_im,&CKonamiCPU::opcode2,&CKonamiCPU::cmps_im,&CKonamiCPU::opcode2,&CKonamiCPU::addd_im,&CKonamiCPU::opcode2,&CKonamiCPU::subd_im,&CKonamiCPU::opcode2,	/* 50 */
		&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::bra    ,&CKonamiCPU::bhi    ,&CKonamiCPU::bcc    ,&CKonamiCPU::bne    ,&CKonamiCPU::bvc    ,&CKonamiCPU::bpl    ,&CKonamiCPU::bge    ,&CKonamiCPU::bgt    ,	/* 60 */
		&CKonamiCPU::lbra   ,&CKonamiCPU::lbhi   ,&CKonamiCPU::lbcc   ,&CKonamiCPU::lbne   ,&CKonamiCPU::lbvc   ,&CKonamiCPU::lbpl   ,&CKonamiCPU::lbge   ,&CKonamiCPU::lbgt   ,
		&CKonamiCPU::brn    ,&CKonamiCPU::bls    ,&CKonamiCPU::bcs    ,&CKonamiCPU::beq    ,&CKonamiCPU::bvs    ,&CKonamiCPU::bmi    ,&CKonamiCPU::blt    ,&CKonamiCPU::ble    ,	/* 70 */
		&CKonamiCPU::lbrn   ,&CKonamiCPU::lbls   ,&CKonamiCPU::lbcs   ,&CKonamiCPU::lbeq   ,&CKonamiCPU::lbvs   ,&CKonamiCPU::lbmi   ,&CKonamiCPU::lblt   ,&CKonamiCPU::lble   ,
		&CKonamiCPU::clra   ,&CKonamiCPU::clrb   ,&CKonamiCPU::opcode2,&CKonamiCPU::coma   ,&CKonamiCPU::comb   ,&CKonamiCPU::opcode2,&CKonamiCPU::nega   ,&CKonamiCPU::negb   ,	/* 80 */
		&CKonamiCPU::opcode2,&CKonamiCPU::inca   ,&CKonamiCPU::incb   ,&CKonamiCPU::opcode2,&CKonamiCPU::deca   ,&CKonamiCPU::decb   ,&CKonamiCPU::opcode2,&CKonamiCPU::rts    ,
		&CKonamiCPU::tsta   ,&CKonamiCPU::tstb   ,&CKonamiCPU::opcode2,&CKonamiCPU::lsra   ,&CKonamiCPU::lsrb   ,&CKonamiCPU::opcode2,&CKonamiCPU::rora   ,&CKonamiCPU::rorb   ,	/* 90 */
		&CKonamiCPU::opcode2,&CKonamiCPU::asra   ,&CKonamiCPU::asrb   ,&CKonamiCPU::opcode2,&CKonamiCPU::asla   ,&CKonamiCPU::aslb   ,&CKonamiCPU::opcode2,&CKonamiCPU::rti    ,
		&CKonamiCPU::rola   ,&CKonamiCPU::rolb   ,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,	/* a0 */
		&CKonamiCPU::opcode2,&CKonamiCPU::opcode2,&CKonamiCPU::bsr    ,&CKonamiCPU::lbsr   ,&CKonamiCPU::decbjnz,&CKonamiCPU::decxjnz,&CKonamiCPU::nop    ,&CKonamiCPU::illegal,
		&CKonamiCPU::abx    ,&CKonamiCPU::daa	   ,&CKonamiCPU::sex    ,&CKonamiCPU::mul    ,&CKonamiCPU::lmul   ,&CKonamiCPU::divx   ,&CKonamiCPU::bmove  ,&CKonamiCPU::move   ,	/* b0 */
		&CKonamiCPU::lsrd   ,&CKonamiCPU::opcode2,&CKonamiCPU::rord   ,&CKonamiCPU::opcode2,&CKonamiCPU::asrd   ,&CKonamiCPU::opcode2,&CKonamiCPU::asld   ,&CKonamiCPU::opcode2,
		&CKonamiCPU::rold   ,&CKonamiCPU::opcode2,&CKonamiCPU::clrd   ,&CKonamiCPU::opcode2,&CKonamiCPU::negd   ,&CKonamiCPU::opcode2,&CKonamiCPU::incd   ,&CKonamiCPU::opcode2,	/* c0 */
		&CKonamiCPU::decd   ,&CKonamiCPU::opcode2,&CKonamiCPU::tstd   ,&CKonamiCPU::opcode2,&CKonamiCPU::absa   ,&CKonamiCPU::absb   ,&CKonamiCPU::absd   ,&CKonamiCPU::bset   ,
		&CKonamiCPU::bset2  ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* d0 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* e0 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* f0 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal
	};

	void (CKonamiCPU::*konami_indexed[0x100])() = {
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* 00 */
		&CKonamiCPU::leax   ,&CKonamiCPU::leay   ,&CKonamiCPU::leau   ,&CKonamiCPU::leas   ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::lda_ix ,&CKonamiCPU::ldb_ix ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::adda_ix,&CKonamiCPU::addb_ix,	/* 10 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::adca_ix,&CKonamiCPU::adcb_ix,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::suba_ix,&CKonamiCPU::subb_ix,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::sbca_ix,&CKonamiCPU::sbcb_ix,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::anda_ix,&CKonamiCPU::andb_ix,	/* 20 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::bita_ix,&CKonamiCPU::bitb_ix,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::eora_ix,&CKonamiCPU::eorb_ix,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::ora_ix ,&CKonamiCPU::orb_ix ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::cmpa_ix,&CKonamiCPU::cmpb_ix,	/* 30 */
		&CKonamiCPU::illegal,&CKonamiCPU::setline_ix,&CKonamiCPU::sta_ix,&CKonamiCPU::stb_ix,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::ldd_ix ,&CKonamiCPU::illegal,&CKonamiCPU::ldx_ix ,&CKonamiCPU::illegal,&CKonamiCPU::ldy_ix ,&CKonamiCPU::illegal,&CKonamiCPU::ldu_ix ,	/* 40 */
		&CKonamiCPU::illegal,&CKonamiCPU::lds_ix ,&CKonamiCPU::illegal,&CKonamiCPU::cmpd_ix,&CKonamiCPU::illegal,&CKonamiCPU::cmpx_ix,&CKonamiCPU::illegal,&CKonamiCPU::cmpy_ix,
		&CKonamiCPU::illegal,&CKonamiCPU::cmpu_ix,&CKonamiCPU::illegal,&CKonamiCPU::cmps_ix,&CKonamiCPU::illegal,&CKonamiCPU::addd_ix,&CKonamiCPU::illegal,&CKonamiCPU::subd_ix,	/* 50 */
		&CKonamiCPU::std_ix ,&CKonamiCPU::stx_ix ,&CKonamiCPU::sty_ix ,&CKonamiCPU::stu_ix ,&CKonamiCPU::sts_ix ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* 60 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* 70 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::clr_ix ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::com_ix ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* 80 */
		&CKonamiCPU::neg_ix ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::inc_ix ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::dec_ix ,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::tst_ix ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::lsr_ix ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* 90 */
		&CKonamiCPU::ror_ix ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::asr_ix ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::asl_ix ,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::rol_ix,&CKonamiCPU::lsrw_ix,&CKonamiCPU::rorw_ix,&CKonamiCPU::asrw_ix,&CKonamiCPU::aslw_ix,&CKonamiCPU::rolw_ix,	/* a0 */
		&CKonamiCPU::jmp_ix ,&CKonamiCPU::jsr_ix ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* b0 */
		&CKonamiCPU::illegal,&CKonamiCPU::lsrd_ix,&CKonamiCPU::illegal,&CKonamiCPU::rord_ix,&CKonamiCPU::illegal,&CKonamiCPU::asrd_ix,&CKonamiCPU::illegal,&CKonamiCPU::asld_ix,
		&CKonamiCPU::illegal,&CKonamiCPU::rold_ix,&CKonamiCPU::illegal,&CKonamiCPU::clrw_ix,&CKonamiCPU::illegal,&CKonamiCPU::negw_ix,&CKonamiCPU::illegal,&CKonamiCPU::incw_ix,	/* c0 */
		&CKonamiCPU::illegal,&CKonamiCPU::decw_ix,&CKonamiCPU::illegal,&CKonamiCPU::tstw_ix,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* d0 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* e0 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* f0 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal
	};

	void (CKonamiCPU::*konami_direct[0x100])() = {
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* 00 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::lda_di ,&CKonamiCPU::ldb_di ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::adda_di,&CKonamiCPU::addb_di,	/* 10 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::adca_di,&CKonamiCPU::adcb_di,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::suba_di,&CKonamiCPU::subb_di,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::sbca_di,&CKonamiCPU::sbcb_di,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::anda_di,&CKonamiCPU::andb_di,	/* 20 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::bita_di,&CKonamiCPU::bitb_di,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::eora_di,&CKonamiCPU::eorb_di,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::ora_di ,&CKonamiCPU::orb_di ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::cmpa_di,&CKonamiCPU::cmpb_di,	/* 30 */
		&CKonamiCPU::illegal,&CKonamiCPU::setline_di,&CKonamiCPU::sta_di,&CKonamiCPU::stb_di,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::ldd_di ,&CKonamiCPU::illegal,&CKonamiCPU::ldx_di ,&CKonamiCPU::illegal,&CKonamiCPU::ldy_di ,&CKonamiCPU::illegal,&CKonamiCPU::ldu_di ,	/* 40 */
		&CKonamiCPU::illegal,&CKonamiCPU::lds_di ,&CKonamiCPU::illegal,&CKonamiCPU::cmpd_di,&CKonamiCPU::illegal,&CKonamiCPU::cmpx_di,&CKonamiCPU::illegal,&CKonamiCPU::cmpy_di,
		&CKonamiCPU::illegal,&CKonamiCPU::cmpu_di,&CKonamiCPU::illegal,&CKonamiCPU::cmps_di,&CKonamiCPU::illegal,&CKonamiCPU::addd_di,&CKonamiCPU::illegal,&CKonamiCPU::subd_di,	/* 50 */
		&CKonamiCPU::std_di ,&CKonamiCPU::stx_di ,&CKonamiCPU::sty_di ,&CKonamiCPU::stu_di ,&CKonamiCPU::sts_di ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* 60 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* 70 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::clr_di ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::com_di ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* 80 */
		&CKonamiCPU::neg_di ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::inc_di ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::dec_di ,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::tst_di ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::lsr_di ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* 90 */
		&CKonamiCPU::ror_di ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::asr_di ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::asl_di ,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::rol_di ,&CKonamiCPU::lsrw_di,&CKonamiCPU::rorw_di,&CKonamiCPU::asrw_di,&CKonamiCPU::aslw_di,&CKonamiCPU::rolw_di,	/* a0 */
		&CKonamiCPU::jmp_di ,&CKonamiCPU::jsr_di ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* b0 */
		&CKonamiCPU::illegal,&CKonamiCPU::lsrd_di,&CKonamiCPU::illegal,&CKonamiCPU::rord_di,&CKonamiCPU::illegal,&CKonamiCPU::asrd_di,&CKonamiCPU::illegal,&CKonamiCPU::asld_di,
		&CKonamiCPU::illegal,&CKonamiCPU::rold_di,&CKonamiCPU::illegal,&CKonamiCPU::clrw_di,&CKonamiCPU::illegal,&CKonamiCPU::negw_di,&CKonamiCPU::illegal,&CKonamiCPU::incw_di,	/* c0 */
		&CKonamiCPU::illegal,&CKonamiCPU::decw_di,&CKonamiCPU::illegal,&CKonamiCPU::tstw_di,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* d0 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* e0 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* f0 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal
	};

	void (CKonamiCPU::*konami_extended[0x100])() = {
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* 00 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::lda_ex ,&CKonamiCPU::ldb_ex ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::adda_ex,&CKonamiCPU::addb_ex,	/* 10 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::adca_ex,&CKonamiCPU::adcb_ex,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::suba_ex,&CKonamiCPU::subb_ex,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::sbca_ex,&CKonamiCPU::sbcb_ex,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::anda_ex,&CKonamiCPU::andb_ex,	/* 20 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::bita_ex,&CKonamiCPU::bitb_ex,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::eora_ex,&CKonamiCPU::eorb_ex,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::ora_ex ,&CKonamiCPU::orb_ex ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::cmpa_ex,&CKonamiCPU::cmpb_ex,	/* 30 */
		&CKonamiCPU::illegal,&CKonamiCPU::setline_ex,&CKonamiCPU::sta_ex,&CKonamiCPU::stb_ex,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::ldd_ex ,&CKonamiCPU::illegal,&CKonamiCPU::ldx_ex ,&CKonamiCPU::illegal,&CKonamiCPU::ldy_ex ,&CKonamiCPU::illegal,&CKonamiCPU::ldu_ex ,	/* 40 */
		&CKonamiCPU::illegal,&CKonamiCPU::lds_ex ,&CKonamiCPU::illegal,&CKonamiCPU::cmpd_ex,&CKonamiCPU::illegal,&CKonamiCPU::cmpx_ex,&CKonamiCPU::illegal,&CKonamiCPU::cmpy_ex,
		&CKonamiCPU::illegal,&CKonamiCPU::cmpu_ex,&CKonamiCPU::illegal,&CKonamiCPU::cmps_ex,&CKonamiCPU::illegal,&CKonamiCPU::addd_ex,&CKonamiCPU::illegal,&CKonamiCPU::subd_ex,	/* 50 */
		&CKonamiCPU::std_ex ,&CKonamiCPU::stx_ex ,&CKonamiCPU::sty_ex ,&CKonamiCPU::stu_ex ,&CKonamiCPU::sts_ex ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* 60 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* 70 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::clr_ex ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::com_ex ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* 80 */
		&CKonamiCPU::neg_ex ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::inc_ex ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::dec_ex ,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::tst_ex ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::lsr_ex ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* 90 */
		&CKonamiCPU::ror_ex ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::asr_ex ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::asl_ex ,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::rol_ex ,&CKonamiCPU::lsrw_ex,&CKonamiCPU::rorw_ex,&CKonamiCPU::asrw_ex,&CKonamiCPU::aslw_ex,&CKonamiCPU::rolw_ex,	/* a0 */
		&CKonamiCPU::jmp_ex ,&CKonamiCPU::jsr_ex ,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* b0 */
		&CKonamiCPU::illegal,&CKonamiCPU::lsrd_ex,&CKonamiCPU::illegal,&CKonamiCPU::rord_ex,&CKonamiCPU::illegal,&CKonamiCPU::asrd_ex,&CKonamiCPU::illegal,&CKonamiCPU::asld_ex,
		&CKonamiCPU::illegal,&CKonamiCPU::rold_ex,&CKonamiCPU::illegal,&CKonamiCPU::clrw_ex,&CKonamiCPU::illegal,&CKonamiCPU::negw_ex,&CKonamiCPU::illegal,&CKonamiCPU::incw_ex,	/* c0 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* d0 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* e0 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,	/* f0 */
		&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,&CKonamiCPU::illegal,
	};

	memcpy(this->konami_main,konami_main,sizeof(konami_main));
	memcpy(this->konami_extended,konami_extended,sizeof(konami_extended));
	memcpy(this->konami_direct,konami_direct,sizeof(konami_direct));
	memcpy(this->konami_indexed,konami_indexed,sizeof(konami_indexed));

	this->clk = clock;

	Reset();
	return 0;
}

void CKonamiCPU::AssignMMU(int slot, CBaseMMU *mmu) {
	this->mmu = mmu;
}

int CKonamiCPU::GetClock() {
	return this->clk;
}

void CKonamiCPU::CheckIRQs() {
	if (this->nmi_pending && (this->int_state & KONAMI_LDS))
	{
		this->nmi_pending = FALSE;

		/* state already saved by CWAI? */
		if (this->int_state & KONAMI_CWAI)
		{
			this->int_state &= ~KONAMI_CWAI;
			this->icount -= 7;
	    }
		else
		{
			CC |= CC_E; 				/* save entire state */
			PUSHWORD(cpustate, pPC);
			PUSHWORD(cpustate, pU);
			PUSHWORD(cpustate, pY);
			PUSHWORD(cpustate, pX);
			PUSHBYTE(cpustate, DP);
			PUSHBYTE(cpustate, B);
			PUSHBYTE(cpustate, A);
			PUSHBYTE(cpustate, CC);
			this->icount -= 19;
		}
		CC |= CC_IF | CC_II;			/* inhibit FIRQ and IRQ */
		PCD = RM16(0xfffc);
		//(void)(*cpustate->irq_callback)(cpustate->device, INPUT_LINE_NMI);
	}

	else if (this->irq_state[KONAMI_FIRQ_LINE] !=CLEAR_LINE && !(CC & CC_IF))
	{
		/* fast IRQ */
		/* state already saved by CWAI? */
		if (this->int_state & KONAMI_CWAI)
		{
			this->int_state &= ~KONAMI_CWAI;  /* clear CWAI */
			this->icount -= 7;
        }
		else
		{
			CC &= ~CC_E;				/* save 'short' state */
			PUSHWORD(this, pPC);
			PUSHBYTE(this, CC);
			this->icount -= 10;
		}
		CC |= CC_IF | CC_II;			/* inhibit FIRQ and IRQ */
		PCD = RM16(0xfff6);
		//(void)(*cpustate->irq_callback)(cpustate->device, KONAMI_FIRQ_LINE);
	}

	else if (this->irq_state[KONAMI_IRQ_LINE] != CLEAR_LINE && !(CC & CC_II))
	{
		/* standard IRQ */
		/* state already saved by CWAI? */
		if (this->int_state & KONAMI_CWAI)
		{
			this->int_state &= ~KONAMI_CWAI;  /* clear CWAI flag */
			this->icount -= 7;
		}
		else
		{
			CC |= CC_E; 				/* save entire state */
			PUSHWORD(cpustate, pPC);
			PUSHWORD(cpustate, pU);
			PUSHWORD(cpustate, pY);
			PUSHWORD(cpustate, pX);
			PUSHBYTE(cpustate, DP);
			PUSHBYTE(cpustate, B);
			PUSHBYTE(cpustate, A);
			PUSHBYTE(cpustate, CC);
			this->icount -= 19;
		}
		CC |= CC_II;					/* inhibit IRQ */
		PCD = RM16(0xfff8);
		// fuck you thanks
		//(void)(*cpustate->irq_callback)(cpustate->device, KONAMI_IRQ_LINE);
	}
}

void CKonamiCPU::Reset() {


	int_state = 0;
	nmi_state = CLEAR_LINE;
	nmi_pending = FALSE;
	irq_state[0] = CLEAR_LINE;
	irq_state[1] = CLEAR_LINE;

	DPD = 0;			/* Reset direct page register */

    CC |= CC_II;        /* IRQ disabled */
    CC |= CC_IF;        /* FIRQ disabled */

	PCD = RM16(0xfffe);

}

void CKonamiCPU::Interrupt(int line, int state, int vector) {
	Interrupt(line,state);
}

void CKonamiCPU::Interrupt(int irqline, int state) {
	if (state != CLEAR_LINE) int_state &= ~KONAMI_SYNC;

	if (irqline == INPUT_LINE_NMI)
	{
		if (nmi_state == CLEAR_LINE && state != CLEAR_LINE)
			nmi_pending = TRUE;
		nmi_state = state;
	} else if (irqline < 2)
		irq_state[irqline] = state;

}

int CKonamiCPU::Exec(int cycles) {

	icount = cycles;
	CheckIRQs();

	if( int_state & (KONAMI_CWAI | KONAMI_SYNC) )
	{
		icount = 0;
	}
	else
	{
		do
		{
			UINT8 ireg;

			pPPC = pPC;

			//debugger_instruction_hook(device, PCD);

			ireg = ireg = ROP(cpustate, PCD);
			PC++;

            (this->*konami_main[ireg])();

            icount -= cycles1[ireg];

        } while( icount > 0 );
    }

	return cycles - icount;
}

// Factories and aliases
CPU_FACTORY("KONAMI",1,CKonamiCPU);
CPU_FACTORY_ALIAS("K052001",1,K52001,CKonamiCPU); // Konami 052001, one revision of the CPU
